package com.qiyang.login.controller;

import com.qiyang.api.log.LogApi;
import com.qiyang.api.uaa.AuthrizationApi;
import com.qiyang.login.dao.entity.SysUser;
import com.qiyang.login.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

/**
 * Created on 2018/4/29.
 *
 * @author sehuaqi.
 * 描述：请求控制层 C
 */
@RestController
@RequestMapping("/login")
@RefreshScope
public class LoginController {


    @Autowired
    private RestTemplate template;

    /*当你的FeignClient接口不在你APP所在包下时，一定记得要在APP配扫描包的路径哈*/
    @Autowired
    private LogApi logApi;

    @Autowired
    @Qualifier(value = "authrizationApi1")
    private AuthrizationApi authrizationApi1;

    @Autowired
    @Qualifier(value = "authrizationApi2")
    private AuthrizationApi authrizationApi2;

    @Autowired
    private LoginService loginService;

    @Value("${dev.name}")
    private String infoName;

    /**
     * 处理用户登录
     *
     * @param username
     * @param password
     * @return
     */
    @PostMapping("")
    public String login(String username, String password) {

        //String status = template.getForObject("http://service-log/log/printLog?info=" + password, String.class);

        String status = logApi.printLog(username + "," + password);

        SysUser sysUser = loginService.login(username, password);

        //调用权限服务，获取用户角色

        if (sysUser == null) {
            return "登录失败，用户名或密码错误";
        } else {
            String roleCode = authrizationApi1.role(sysUser.getId());
            roleCode = authrizationApi2.role(sysUser.getId());
            return "登录" + status + "，欢迎：" + sysUser.getNickname() + "，角色为：" + roleCode;
        }

    }

    @RequestMapping("infoName")
    public String infoName(){
        return infoName;
    }

}
